﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using static UnityEngine.EventSystems.EventTrigger;

public class StoryPlay : MonoBehaviour
{
    //脚本中可能会调用到的其他组件
    [Header("输入组件")]
    [SerializeField] StoryInput input;
    [Header("UI组件")]
    [SerializeField] List<Image> images;
    [SerializeField] Text Lname;
    [SerializeField] Text Rname;
    [SerializeField] Text Storytext;

    
    [SerializeField] GameObject DivergenceOptionMenu;
    [SerializeField] GameObject DivergenceOptionPanel;
    [SerializeField] List<GameObject> DivergenceOption;

    //脚本运行前需要填充完成的数据
    //必须填充
    Story story;
    //在导入剧情时进行填充
    ShortStory shortStory;
    string[] order_ch;
    Dictionary<int, DivergenceOptionData> order_Option;
    Dictionary<string, StoryCharacterData> characterData;

    //脚本运行时用到的属性
    int i;
    bool playing = true;

    


    /*****************************************************/
    //固定的在特定条件会调用的各种方法
    /*****************************************************/
    private void Awake()
    {        
        gameObject.SetActive(false);
    }

    private void OnEnable()
    {
        DivergenceOptionMenu.SetActive(false);
        input.onStoryPlay += NextSentence;        
        input.EnableStoryPlaying();         //启动剧情播放动作表
    }

    private void OnDisable()
    {
        input.DisableAllInputs();           //关闭剧情播放动作表
        input.onStoryPlay -= NextSentence;        
        playing = false;
    }


    /*****************************************************/
    //自定义的方法
    /*****************************************************/

    //初始化各种信息
    public void Instantiate(Story s)
    {
        i = 0;

        //初始化各种信息
        story = s;//加载对应id的story
        characterData = StoryDataManager.Instance.LoadCharactorData(story);

        gameObject.SetActive(true);
        NextSentence();
    }

    //播放下一段剧情
    //speaker为模式选择，0为无插图的旁白，1为左侧显示，2为右侧显示
    public void NextSentence()
    {
        List<ShortStory> shortStories = story.shortStory;
        if (i < shortStories.Count)
        {
            //Debug.Log(i+"  /"+ shortStories.Count);
            switch (shortStories[i].type)
            {
                case 1:
                    NextSentence_1();
                    i++;
                    break;
                case 2:                  
                    NextSentence_2();
                    i++;
                    NextSentence();
                    break;
                case 3:
                    NextSentence_3();
                    i++;
                    NextSentence();
                    break;
            }   
        }
        else
            gameObject.SetActive(false);
    }

    //更换相机中显示的CG与人物立绘
    public void switchImage()
    {
        int i = 0;
        foreach(var v in images)
        {
            if (order_ch[i] != null && order_ch[i].Length > 0)
            {
                v.enabled = true;
                v.sprite = characterData[order_ch[i]].getImage();
            }
            else
                v.enabled = false;
            i++;
        }         
    }

    public void switchText()
    {
        string str = shortStory.sentence;
        //改变对话框中的文字

        Storytext.text = str;
        if (order_ch[1] != null)
            Lname.text = characterData[order_ch[1]].name;
        else
            Lname.text = null;

        if (order_ch[2] != null)
            Rname.text = characterData[order_ch[2]].name;
        else
            Rname.text = null;
    }


    //引用分歧选项
    public void nextDivergenceOption(ShortStory shortStory)
    {
        //关闭所有输入事件
        input.DisableAllInputs();

        //复制按钮并放入合适的位置
        GameObject button = GameObject.Instantiate(DivergenceOption[0]);
        DivergenceOption.Add(button);
        button.transform.SetParent(DivergenceOptionPanel.transform);
        button.GetComponent<RectTransform>().localPosition = new Vector3(0, 0, 0);
        button.GetComponent<RectTransform>().localScale = new Vector3(1, 1, 1);

        //获取按钮的text组件并修改其中的文本
        Text text = button.GetComponentInChildren<Text>().GetComponent<Text>();
        text.text = shortStory.sentence;

        //为按钮加上合适的触发方法
        int type = shortStory.type;
        Option(button, type);

        Debug.Log("选项弹出");
        DivergenceOptionMenu.SetActive(true);
        button.SetActive(true);
    }

    //删除生成的按钮组件
    public void deleteButtons()
    {
        for(int n=0;n< DivergenceOption.Count; n++)
        {
            GameObject.Destroy(DivergenceOption[n]);
        }
    }

    /*******************以下为各种语句播放时将会执行的方法***********************/
    public void NextSentence_1()
    {
        shortStory = story.shortStory[i];
        order_ch = shortStory.order_character;
        switchText();
        //判断需要进行加载的图片
        switchImage();
    }
    public void NextSentence_2()
    {
        shortStory = story.shortStory[i];
        nextDivergenceOption(shortStory);
    }

    public void NextSentence_3()
    {
        shortStory = story.shortStory[i];
        nextDivergenceOption(shortStory);

    }

    /*******************以下为各种分歧选项的方法***********************/
    public void Option(GameObject button, int type)
    {
        Button btn = button.GetComponent<Button>();
        EventTrigger trigger = button.GetComponent<EventTrigger>();
        Entry entry = new Entry();
        entry.eventID = EventTriggerType.PointerClick;
        entry.callback = new EventTrigger.TriggerEvent();

        // 鼠标点击事件
        switch (type)
        {
            case 1:
                entry.callback.AddListener(Option1);
                break;
            case 2:
                entry.callback.AddListener(Option1);
                break;
            case 3:
                entry.callback.AddListener(Option3);
                break;
        }
        trigger.triggers.Add(entry);

        

        DivergenceOptionMenu.SetActive(true);
    }


    private void Option1(BaseEventData pointData)
    {
        input.EnableStoryPlaying();
        DivergenceOptionMenu.SetActive(false);
        deleteButtons();
        NextSentence();
    }

    private void Option3(BaseEventData pointData)
    {
        input.EnableStoryPlaying();
        DivergenceOptionMenu.SetActive(false);
        deleteButtons();
        StoryManager.Instance.StoryPlaying(shortStory.jumpLine);
    }









}
